import axios from "axios";
import qs from 'qs';
import {getBaseVue} from "@/main";

``

const baseUrl = process.env.NODE_ENV==="production"?"http://192.168.234.150:8080/gadgets/":"http://127.0.0.1:8081/";
const successCode = "0000000000";
const dictMap = new Map;
let sessionId = " ";
/**
 * 查询代码示例�
 * ----------------按钮单击查询事件------------------s
 * queryBtnClick:function (){s
      getRequest(// 此处可以使用get或post
        "manage/sequeuce/querySeque.do",
        this.$refs["queryForm"].$data.submitParams,
        this.parseQueryResult,
        null,
        this
      );
    },
 * ----------------数据渲染方法------------------
    parseQueryResult:function (data,component) {
      let msg = data.data.errorMsg;
      let arr = JSON.parse(msg);
      component.$props["inputArr"]=arr;
    }
 */

export function setSessionId(session){
    sessionId = session;
}

export function getRequest(url,param,successCallBack,fallCallBack,component){
    param["gadgetsSessionId"] = sessionId;
    axios.get(baseUrl+url,qs.stringify(param)).then((response)=>{
        if(response.data.retCode === successCode){
            successCallBack.call(this,response,component);
        }else{
            if(undefined!==fallCallBack && null!==fallCallBack) {
                fallCallBack.call(this, response, component);
            }
        }
    }).catch((error)=>{
        if(error.response.status===399){
            let baseVue = getBaseVue();
            baseVue.$router.push("/Login");
        }
    });
}

export function postRequest(url,param,successCallBack,fallCallBack,component){
    if(undefined===param){
        param = {};
    }
    param["gadgetsSessionId"] = sessionId;
    axios.post(baseUrl+url,qs.stringify(param)).then((response)=>{
        if(undefined===response){
            fallCallBack.call(this, response, component);
        }else{
            if(undefined!==successCallBack && response.data.retCode === successCode){
                successCallBack.call(this,response,component);
            }else{
                if(undefined!==fallCallBack && null!==fallCallBack) {
                    fallCallBack.call(this, response, component);
                }
            }
        }
    }).catch((error)=>{
        if(error.response.status===399){
            let baseVue = getBaseVue();
            baseVue.$router.push("/Login");
        }
    });
}

export function postRequestForComplex(url,param,successCallBack,fallCallBack,component){
    param["gadgetsSessionId"] = sessionId;
    axios.post(baseUrl+url,qs.stringify(param),complexJsonConfig).then((response)=>{
        if(response.data.retCode !== successCode){
            successCallBack.call(this,response,component);
        }else{
            if(undefined!==fallCallBack && null!==fallCallBack){
                fallCallBack.call(this,response,component);
            }
        }
    }).catch((error)=>{
        if(error.response.status===399){
            let baseVue = getBaseVue();
            baseVue.$router.push("/Login");
        }
    });
}

export function postRequestForUpload(url,param,successCallBack,fallCallBack,component){
    param["gadgetsSessionId"] = sessionId;
    axios.post(baseUrl+url,param,multipleConfig).then((response)=>{
        if(response.data.retCode !== successCode){
            successCallBack.call(this,response,component);
        }else{
            if(undefined!==fallCallBack && null!==fallCallBack) {
                fallCallBack.call(this, response, component);
            }
        }
    }).catch((error)=>{
        if(error.response.status===399){
            let baseVue = getBaseVue();
            baseVue.$router.push("/Login");
        }
    });
}

let multipleConfig={
    headers: {
        'Content-Type': 'multipart/form-data;boundary =' + new Date().getTime()
    }
};

let complexJsonConfig={
    headers: {
        'Content-Type': 'application/json'
    }
};
